#!/usr/bin/python
# -*- coding:utf-8 -*-
import ConfigParser
from datetime import datetime

import MySQLdb


class HtmlOuter(object):
    def __init__(self):
        cf = ConfigParser.ConfigParser()
        cf.read('config.ini')
        host = cf.get('db', 'host')
        user = cf.get('db', 'user')
        pwd = cf.get('db', 'passwd')
        db = cf.get('db', 'db')
        self.conn = MySQLdb.connect(host=host, user=user, passwd=pwd, db=db, charset="utf8")
        self.cursor = self.conn.cursor()

    def write(self, datas):
        try:
            for data in datas:
                self.__write(data)
        except Exception, e:
            print e
            print data

    def __write(self, data):
        sql = 'insert into talents_sichuang('
        names = data.keys()
        for name in names:
            sql += name + ','
        sql = sql[:-1] + ') values('
        for name in names:
            sql += '"%s",' % data[name].replace('\n', ' ')
        sql = sql[:-1] + ')'
        print sql
        if self.cursor.execute(sql) == 1:
            try:
                self.conn.commit()
            except MySQLdb.Error:
                return None
        else:
            return None
